<template>
  <el-dialog :visible.sync="opened" @close="close" :lock-scroll="false" append-to-body>
    <template #title>
      <div class="dialog-title">
        <div class="title">{{title}}</div>
      </div>
    </template>
    <div class="dialog-container">
      <div class="content">
        <el-checkbox-group 
          v-model="checked"
          :min="0"
          :max="num">
          <el-checkbox v-for="(item, index) in data" :label="index" :key="index">{{item.label}}</el-checkbox>
        </el-checkbox-group>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveHandle">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'SelectCoupon',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    num: {
      type: Number,
      default: 1
    }
  },
  data () {
    return {
      title: '关联优惠券',
      opened: false,
      checked: [],
      data: [
        {
          limit: 999,
          discount: 199,
          label: '满999减199'
        },
        {
          limit: 999,
          discount: 199,
          label: '满999减199'
        },
        {
          limit: 999,
          discount: 199,
          label: '满999减199'
        },{
          limit: 999,
          discount: 199,
          label: '满999减199'
        },
        {
          limit: 999,
          discount: 199,
          label: '满999减199'
        }
      ]
    }
  },
  watch: {
    visible (val) {
      this.opened = !!val
    },
    num () {
      this.checked = []
    }
  },
  methods: {
    close () {
      this.$emit('update:visible', false)
    },
    saveHandle () {
      if (this.checked.length < this.num) {
        return this.$message.warning(`请选择${this.num}张优惠券`)
      } else {
        this.$emit('select', this.checked.map(item => ({ ...this.data[item] })))
        this.close()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.dialog-title {
  .title {
    font-size: 18px;
    color: #333333;
    font-weight: bold;
    padding-bottom: 10px;
    border-bottom: 1px dashed #eeeded;
  }
}
.dialog-container {
  overflow: hidden;
  .content {
    overflow: hidden;
  }
}
</style>